package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Consulter;
import com.woniuxy.oldmanapi.entity.Cost;
import com.woniuxy.oldmanapi.entity.Othercost;
import com.woniuxy.oldmanapi.service.ConsultService;
import com.woniuxy.oldmanapi.service.CostService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class CostController {
    @Autowired
    CostService costService;

    //查询咨询者信息
    @GetMapping("/cost/selectCost")
    public ResponseResult<PageInfo<Cost>> selectConsulter(int now, int size, String cname){
        ResponseResult<PageInfo<Cost>> result = new ResponseResult<>();
        try {
            PageInfo<Cost> pageInfo = costService.selectCost(now, size, cname);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //更新咨询者信息
    @PostMapping("/cost/updateCost")
    public ResponseResult<Void> updateConsulter(@RequestBody Cost consulter){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            costService.updateCost(consulter);
            result.setCode(200);
            result.setMsg("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //删除咨询者信息
    @GetMapping("/cost/deleteCost/{id}")
    public ResponseResult<Void> deleteCost(@PathVariable(value = "id") int id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            costService.deleteCost(id);
            result.setCode(200);
            result.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }

    //新增咨询者信息
    @PostMapping("/cost/insertCost")
    public ResponseResult<Void> insertConsulter(@RequestBody Cost consulter){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            costService.insertCost(consulter);
            result.setCode(200);
            result.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(203);
            result.setMsg("系统错误，请联系管理员!!!");
        }
        return result;
    }
}
